/*
 * 
 * Copyright (C) 2010 Wilfried Elmenreich, Anita Sobe 
 * This file is part of VideoNetwork.
 * 
 * VideoNetwork is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>. 
 */
package proxy;

import java.util.Random;

public class VideoUnit implements Comparable<VideoUnit> {
	private int sizeInBytes;
	static final double videoBitRate = 200000;
	private int id;
	static final int minSize = 50000;
	static final int maxSize = 5000000;
	static final int avgSize = 500000;
	static final int stdevSize = 500000;
	private static int nextid = 0;
	

	/**
	 * The array content simulates a vector of different content components. For
	 * example content[0] could refer to a news component, content[1] to
	 * entertainment component and so on. It is used to simulate similarity of
	 * video units.
	 */
	public int[] content;
	
	private double distance;

	public VideoUnit(int contentTypes, Random rand) {
		id = nextid++;
		sizeInBytes = (int) (rand.nextGaussian() * stdevSize + avgSize);
		if (sizeInBytes < minSize)
			sizeInBytes = minSize;
		if (sizeInBytes > maxSize)
			sizeInBytes = maxSize;
		content = new int[contentTypes];
		int sum = 0;
		for (int i = 0; i < contentTypes; i++) {
			content[i] = rand.nextInt(100);
			sum += content[i];
		}
		// normalize content vector to average length of 100
		if (sum == 0)
			sum++;
		for (int i = 0; i < contentTypes; i++)
			content[i] = content[i] * 100 / sum;

	}



	public void calculateDistance(int[] likesContent) {
		double dist = 0;
		for (int i = 0; i < likesContent.length; i++)
			dist += Math.pow(likesContent[i] - content[i], 2);
		distance = Math.sqrt(dist);
	}

	public int getSizeInBytes() {
		return sizeInBytes;
	}

	public static int getNextid() {
		return nextid;
	}


	public int[] getContent() {
		return content;
	}

	public double getDistance() {
		return distance;
	}

	public static void setNextId(int i) {
		nextid = i;
	}

	public int getId() {
		return id;
	}

	public double playLength() {
		return sizeInBytes * 8.0 / videoBitRate;
	}

	@Override
	public int compareTo(VideoUnit o) {
		return (int) Math.round(this.distance - o.distance);
	}

}
